class GameEngine
  
  attr_reader :players, :historical_scores, :max_score, :commentary
  
  
  def initialize
    @players = Hash.new
#    @players_threads = Hash.new
    @threads = Array.new
    @historical_scores = Hash.new
    @max_score = 1
  end  
  
  def get_player_by_name name
    return @players[name.intern]    
  end
  
  def commentary
    @game.commentary
  end
  
  def status   
    current_scores = @game.scores
    current_scores.each do |key,value|
      @historical_scores[key] << value
    end
    current_scores
  end
  
  def add_player player    
    begin
      
      if !@players.has_key? player.name.intern
        @historical_scores[player.name.intern] = []

        @players[player.name.intern] = player
        
        @game = Game.instance        
        @game.add_player player.name
        player.game= @game
        player.update_strategy "puts '#{player.name} plays default'"
        
#        @players_threads[player.name.intern] = Thread.new{@players[player.name.intern].play}
      else
        raise PlayerExistsError, "Screen Name already in use"
      end     
    end    
  end	
  
  def use_player_strategies
#    @players_threads.each_key do | thread |
#      if thread.status != "run"
#        thread.run unless thread.stop?     
#      end
#    end
    @threads.each do | thread |
      thread.kill
    end
 
    @players.each_value do | player |
     @threads << Thread.new{player.play}
    end   
  end
  
  def update_player_strategy player_name, strategy
#    puts player_name +" : "+strategy
    @players[player_name.intern].update_strategy strategy
  end
end